<?php

/**
 * This is the model class for table "announcement".
 *
 * The followings are the available columns in table 'announcement':
 * @property integer $id
 * @property string $title
 * @property string $content
 * @property string $description
 * @property integer $status
 * @property integer $event_fg
 * @property integer $position
 * @property integer $view_no
 * @property integer $join_no
 * @property string $created
 * @property string $updated
 */
class Announcement extends AnnouncementBase
{
	public $radioEvent;
	public $comment;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Announcement the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, content, created, user_id, event_end_date', 'required'),
			array('status, event_fg, position, view_no, join_no', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>100),
			array('updated', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, content, description, status, event_fg, position, view_no, join_no, created, updated, event_end_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'assigns' => array(self::HAS_MANY, 'AnnouncementAssign', 'announcement_id',
								'joinType' => 'LEFT JOIN'),
			/*'views' => array(self::HAS_MANY, 'AnnouncementView', 'announcement_id',
//								'with' => 'member',
								'joinType' => 'LEFT JOIN'),*/
			'profile' => array(self::BELONGS_TO, 'Users', 'user_id', 'with' => 'profiles'),
			'comments' => array(self::HAS_MANY, 'Comments', 'post_id', 'joinType' => 'INNER JOIN'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Tiêu đề',
			'content' => 'Nội dung',
			'description' => 'Mô tả',
			'status' => 'Tình trạng',
			'event_fg' => 'Sự kiện',
			'position' => 'Vị trí',
			'view_no' => 'Đã xem',
			'join_no' => 'Tham dự',
			'created' => 'Ngày tạo',
			'updated' => 'Ngày sửa',
			'comment' => 'Thảo luận',
			'event_end_date' => 'Ngày hết hạn',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($start = '', $end = '')
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$params = array();

		if ($start != '') {
			 $criteria->addCondition('DATE_FORMAT(t.created, "%Y%m") >= :start');
			 $start	= mb_substr($start, 3, 4) . mb_substr($start, 0, 2);
			 $params[':start'] 	= $start;
		}

		if ($end != '') {
			 $criteria->addCondition('DATE_FORMAT(t.created, "%Y%m") <= :end');
			 $end	= mb_substr($end, 3, 4) . mb_substr($end, 0, 2);
			 $params[':end'] 	= $end;
		}
		$criteria->params = $params;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('event_fg',$this->event_fg);
		$criteria->compare('position',$this->position);
		$criteria->compare('view_no',$this->view_no);
		$criteria->compare('join_no',$this->join_no);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('updated',$this->updated,true);
		$criteria->compare('event_end_date',$this->event_end_date,true);
		$criteria->compare('user_id',$this->user_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function getAnnouncementAndCount($id)
	{
		// Count from assign
		$criteria = new CDbCriteria;
		$criteria->with = array(
					'assigns' => array('select' => 'count(announcement_id) as count_assign,
													SUM(case when purpose_join = 0 then 1 else 0 end) as count_no_reply,
													SUM(case when purpose_join = 1 then 1 else 0 end) as count_no_join,
													SUM(case when purpose_join = 2 then 1 else 0 end) as count_purpose_join,
													SUM(case when view_fg = 1 then 1 else 0 end) as count_view,
													SUM(event_joined) as count_joined', 'announcement_id',
										'joinType' => 'INNER JOIN'),
				);
		if (!is_array($id) && $id != "") {
			$criteria->condition='t.id = :announcement_id';
			$criteria->params=array(':announcement_id'=> $id);
		}
		elseif (is_array($id) && !empty($id)) {
			$criteria->addInCondition('t.id', $id);
		}
		$model = Announcement::model()->find($criteria);

		return $model;
	}
}